﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

using NJ.Common.Extension;
using NJ.Common.Web;
using NJ.Dto.User;
using NJ.Entity;

namespace NJ.Service.User
{
    public class UserService : BaseService, IUserService
    {
        public async Task<Paging> GetPagingAsync(Expression<Func<UserEntity, bool>> whereExpression, int pageIndex, int pageSize)
        {
            whereExpression = whereExpression ?? (t => true);

            var list = dbContext.Set<UserEntity>().Where(whereExpression).AsNoTracking();

            var total = list.CountAsync();

            var result = list.Take(pageSize * pageIndex).Skip(pageSize * (pageIndex - 1)).ToListAsync();

            var paper = new Paging
            {
                pageIndex = pageIndex,
                pageSize = pageSize,
                total = await total,
                data = await result
            };

            return paper;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(UserDto userDto)
        {
            var userEntity = userDto.MapTo<UserEntity>();
            dbContext.Set<UserEntity>().Add(userEntity);

            var result = dbContext.SaveChangesAsync();
            return await result;
        }
        /// <summary>
        /// 查找用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public async Task<UserDto> GetUserAsync(string username, string password = null)
        {
            Expression<Func<UserEntity, bool>> whereExpression = ExprExt.True<UserEntity>();

            if (!string.IsNullOrWhiteSpace(username))
            {
                whereExpression = whereExpression.And(t => t.UserName == username);
            }
            if (!string.IsNullOrWhiteSpace(password))
            {
                whereExpression = whereExpression.And(t => t.Password == password);
            }

            var userEntity = await dbContext.Set<UserEntity>().Where(whereExpression).AsNoTracking().FirstOrDefaultAsync();
            var userDto = userEntity.MapTo<UserDto>();
            return userDto;
        }
        /// <summary>
        /// 查找用户
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<UserDto> GetUserAsync(int UserId)
        {
            var userEntity = await FindAsync<UserEntity>(UserId);
            var userDto = userEntity.MapTo<UserDto>();
            return userDto;
        }
        /// <summary>
        /// 查找用户
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public UserDto GetUser(int UserId)
        {
            var userEntity = dbContext.Set<UserEntity>().Find(UserId);
            var userDto = userEntity.MapTo<UserDto>();
            return userDto;
        }
    }
}
